<?php

namespace app\admin\controller\reply;

use app\admin\controller\Base;
use app\admin\model\Authorizer;
use app\admin\model\Category;
use app\admin\model\ReplyMessage;
use app\admin\model\SubscribeMsgAcc;

/**
 * @author zero
 * @Time 2018/12/15
 *
 * Class Index
 * @package app\admin\controller\reply
 *
 * 关注后回复消息接口
 */
class Subscribe extends Base
{
    // 关注后消息首页
    public function index()
    {
        return $this->fetch('replymessage/subscribe');
    }

    // 获取关注后消息列表
    public function get_msg_list()
    {
        $keyword = $this->request->post('keyword');
        $msg_list = ReplyMessage::whereLike('name', '%' . $keyword . '%')->where('is_subscribe', 1)->select();
        foreach ($msg_list as $msg) {
            if ($msg->type == 'text') {
                $msg->type = '文本';
            } elseif ($msg->type == 'news') {
                $msg->type = '图文';
            }
        }
        return $msg_list;
    }

    // 获取当前关注后消息下绑定的账号界面
    public function acc_info_view()
    {
        $msgid = $this->request->param('msgid');
        $this->assign('msgid', $msgid);
        return $this->fetch('replymessage/acc_info');
    }

    // 获取当前关注后消息下绑定的账号
    public function get_acc_list_with_msg()
    {
        $page = $this->request->post('page', 1);
        $keyword = $this->request->post('keyword');
        $msgid = $this->request->post('msgid');
        // 获取关注后消息绑定公众号列表
        $ghid_list = SubscribeMsgAcc::where('msgid', $msgid)->column('ghid');
        $list = Authorizer::whereIn('ghid', $ghid_list)->whereLike('name|ghid', '%' . $keyword . '%')->limit(($page - 1) * 10, 10)->select();
        $data['total'] = Authorizer::whereIn('ghid', $ghid_list)->whereLike('name|ghid', '%' . $keyword . '%')->count();

        if ($list) {
            foreach ($list as $v) {
                $v->category = Category::where('id', $v->category)->value('categoryname');
                $v->category2 = Category::where('id', $v->category2)->value('categoryname');
            }
            $data['list'] = $list;
            return $data;
        } else {
            return false;
        }
    }

    // 添加账号界面
    public function add_acc_view()
    {
        $msgid = $this->request->param('msgid');
        $this->assign('msgid', $msgid);
        return $this->fetch('replymessage/add_acc');
    }

    // 分页获取消息未绑定的公众号
    public function get_acc_list_without_msg()
    {
        $page = $this->request->post('page');
        $keyword = $this->request->post('keyword');
        $category = $this->request->post('category', '');
        $category2 = $this->request->post('category2', '');
        if ($category !== '') {
            $map[] = ['category', '=', $category];
        }
        if ($category2 !== '') {
            $map[] = ['category2', '=', $category2];
        }
        $map[] = ['delstatus', '=', 1];

        // 获取已绑定关注回复的账号
        $ghid_list = SubscribeMsgAcc::column('ghid');
        // 获取未绑定关注回复的账号
        $list = Authorizer::whereNotIn('ghid', $ghid_list)->where($map)->whereLike('name|ghid', '%' . $keyword . '%')->limit(($page - 1) * 10, 10)->select();
        $data['total'] = Authorizer::whereNotIn('ghid', $ghid_list)->where($map)->whereLike('name|ghid', '%' . $keyword . '%')->count();

        if ($list) {
            foreach ($list as $v) {
                $v->category = Category::where('id', $v->category)->value('categoryname');
                $v->category2 = Category::where('id', $v->category2)->value('categoryname');
            }
            $data['list'] = $list;
            return $data;
        } else {
            return false;
        }
    }

    // 根据分类绑定公众号
    public function add_acc_by_category()
    {
        // 消息id
        $msgid = $this->request->post('msgid');
        $ghid_list = $this->request->post('ghid_list');
        return $this->do_add_acc($ghid_list, $msgid);
    }

    // 根据传入的原始id列表字符串绑定公众号
    public function add_acc_by_ghid()
    {
        // 消息id
        $msgid = $this->request->post('msgid');
        // 获取并处理字符串
        $ghid_list = trim(trim($this->request->post('ghid_list')), ',');
        $ghid_list = explode(',', $ghid_list);
        return $this->do_add_acc($ghid_list, $msgid);
    }

    // 添加绑定公众号逻辑
    public function do_add_acc($ghid_list, $msgid)
    {
        $count = 0;
        // 遍历处理ghid
        foreach ($ghid_list as $ghid) {
            $ghid = trim(trim($ghid), ',');
            if ($gh = Authorizer::get(['ghid' => $ghid])) {
                if (!SubscribeMsgAcc::get(['ghid' => $ghid])) {
                    // 同时保证存在此账号,并且无绑定
                    $record = new SubscribeMsgAcc();
                    $record->msgid = $msgid;
                    $record->ghid = $ghid;
                    if ($record->save()) {
                        $count += 1;
                    }
                }
            }
        }
        // 返回添加成功的数量
        return $count;
    }

    //  删除绑定公众号逻辑
    public function del_acc_list()
    {
        $ghid_list = $this->request->post('ghid_list');
        $msgid = $this->request->post('msgid');

        $count = 0;
        // 遍历查找绑定记录,有则删除,并计数
        foreach ($ghid_list as $ghid) {
            $record = SubscribeMsgAcc::get(['ghid' => $ghid, 'msgid' => $msgid]);
            if ($record) {
                if ($record->delete()) {
                    $count += 1;
                }
            }
        }
        return $count;
    }

    // 清空绑定公众号
    public function clear_acc_list()
    {
        $msgid = $this->request->post('msgid');
        if (SubscribeMsgAcc::where('msgid', $msgid)->delete()) {
            return true;
        } else {
            return false;
        }
    }

    // 删除关注后回复信息
    public function del_msg()
    {
        $msgid = $this->request->param('msgid');
        $msg = ReplyMessage::get($msgid);
        $msg->is_subscribe = 0;
        return $msg->save();
    }
}
